const app = getApp()
// pages/card/card.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    target: null,
    no_person: false,
    pageIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.code) {
      this.setData({ target: options.code });
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.loadUserInfo();
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  loadUserInfo: function () {
    var that = this;
    var code = "18359219330";
    app.postJson({
      url: '/person/card/info?code=' + code
    }, function (result, data) {
      console.log("用户信息:%o", data);
      if (result == "0") {
        if (data) {
          that.setData({ user: data });

          wx.setNavigationBarTitle({
            title: data.name + "的名片"//页面标题为路由参数
          })
        } else {

          if (!that.data.target) {
            wx.showToast({
              title: '初次使用个人名片，请先填写个人信息',
              icon: "none",
              mask: true
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '../index/index'
              })
            }, 1000);
          } else {
            that.setData({
              no_person: true
            })
          }
        }
      } else {
        wx.showToast({
          title: '用户信息加载失败',
          mask: true
        })
      }
    })
  },
  goToIndex: function () {
    wx.navigateTo({
      url: '../index/index'
    })
  },
  callPhone: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.user.phone
    })
  },
  copyValueAndLaunchQQ() {
    var that = this;
    wx.setClipboardData({
      data: that.data.user.qq,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: 'QQ号码已经复制到粘贴板，请打开QQ添加好友',
              icon: 'none'
            })
          }
        })
      }
    })
  },
  copyValueAndLaunchWechat() {
    var that = this;
    wx.setClipboardData({
      data: that.data.user.wechat,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '微信号已经复制到粘贴板，请打开微信添加好友界面添加',
              icon: 'none'
            })
          }
        })
      }
    })
  },
  onShareAppMessage: function (res) {
    if (res.from == "menu") {
      return {
        title: "我是" + this.data.user.name,
        path: 'pages/card1/card1?code=' + this.data.user.code,
        imgUrl: this.data.user.userLogo,
        success: function (res) {
          wx.showToast({
            title: '分享成功',
          })
        },
        fail(re) {
          wx.showToast({
            title: '分享失败',
          })
          console.log("分享失败：%o", re);
        }
      }
    }
  },

  pageChange() {
    var pageIndex = (this.data.pageIndex+1) % 2;
    this.setData({
      "pageIndex": pageIndex
    })

  },

  // 触摸开始事件 
  touchStart: function (e) {
    e.preventDefault();
  },
  // 触摸移动事件 
  touchMove: function (e) {
    e.preventDefault();
  },
  // 触摸结束事件 
  touchEnd: function (e) {
    e.preventDefault();
  }
})